/*     */ package com.zimbra.cs.store;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.zmime.ZSharedFileInputStream;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.util.zip.GZIPInputStream;
/*     */ import org.apache.commons.io.FileUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Blob
/*     */ {
/*     */   private File file;
/*     */   private String path;
/*  47 */   private Boolean compressed = null;
/*     */   private String digest;
/*     */   private Long rawSize;
/*     */   
/*     */   protected Blob(File file) {
/*  52 */     if (file == null) {
/*  53 */       throw new NullPointerException("file cannot be null");
/*     */     }
/*     */     
/*  56 */     this.file = file;
/*  57 */     this.path = file.getAbsolutePath();
/*     */   }
/*     */   
/*     */   protected Blob(File file, long rawSize, String digest) {
/*  61 */     this(file);
/*  62 */     this.rawSize = Long.valueOf(rawSize);
/*  63 */     this.digest = digest;
/*     */   }
/*     */   
/*     */   public File getFile() {
/*  67 */     return this.file;
/*     */   }
/*     */   
/*     */   public String getPath() {
/*  71 */     return this.path;
/*     */   }
/*     */   
/*     */   public InputStream getInputStream() throws IOException {
/*  75 */     InputStream in = new ZSharedFileInputStream(this.file);
/*  76 */     if (isCompressed()) {
/*  77 */       in = new GZIPInputStream(in);
/*     */     }
/*  79 */     return in;
/*     */   }
/*     */   
/*     */   public boolean isCompressed() throws IOException {
/*  83 */     if (this.compressed == null) {
/*  84 */       if ((this.rawSize != null) && (this.rawSize.longValue() == this.file.length())) {
/*  85 */         this.compressed = Boolean.FALSE;
/*     */       } else {
/*  87 */         this.compressed = Boolean.valueOf(FileUtil.isGzipped(this.file));
/*     */       }
/*     */     }
/*  90 */     return this.compressed.booleanValue();
/*     */   }
/*     */   
/*     */   public String getDigest()
/*     */     throws IOException
/*     */   {
/*  96 */     if (this.digest == null) {
/*  97 */       initializeSizeAndDigest();
/*     */     }
/*  99 */     return this.digest;
/*     */   }
/*     */   
/*     */   public long getRawSize()
/*     */     throws IOException
/*     */   {
/* 105 */     if (this.rawSize == null) {
/* 106 */       if (!isCompressed()) {
/* 107 */         this.rawSize = Long.valueOf(this.file.length());
/*     */       } else {
/* 109 */         initializeSizeAndDigest();
/*     */       }
/*     */     }
/* 112 */     return this.rawSize.longValue();
/*     */   }
/*     */   
/*     */   private void initializeSizeAndDigest() throws IOException {
/* 116 */     InputStream in = null;
/*     */     
/*     */     try
/*     */     {
/* 120 */       in = getInputStream();
/* 121 */       MessageDigest md = MessageDigest.getInstance("SHA-256");
/* 122 */       byte[] buffer = new byte['Ѐ'];
/*     */       
/* 124 */       long totalBytes = 0L;
/* 125 */       int numBytes; while ((numBytes = in.read(buffer)) >= 0) {
/* 126 */         md.update(buffer, 0, numBytes);
/* 127 */         totalBytes += numBytes;
/*     */       }
/* 129 */       this.digest = ByteUtil.encodeFSSafeBase64(md.digest());
/* 130 */       this.rawSize = Long.valueOf(totalBytes);
/*     */     }
/*     */     catch (NoSuchAlgorithmException e)
/*     */     {
/* 134 */       throw new RuntimeException(e);
/*     */     } finally {
/* 136 */       ByteUtil.closeStream(in);
/*     */     }
/*     */   }
/*     */   
/*     */   public Blob setCompressed(boolean isCompressed) {
/* 141 */     this.compressed = Boolean.valueOf(isCompressed);
/* 142 */     return this;
/*     */   }
/*     */   
/*     */   public Blob setDigest(String digest) {
/* 146 */     this.digest = digest;
/* 147 */     return this;
/*     */   }
/*     */   
/*     */   public Blob setRawSize(long rawSize) {
/* 151 */     this.rawSize = Long.valueOf(rawSize);
/* 152 */     return this;
/*     */   }
/*     */   
/*     */   public Blob copyCachedDataFrom(Blob other) {
/* 156 */     if ((this.compressed == null) && (other.compressed != null)) {
/* 157 */       this.compressed = other.compressed;
/*     */     }
/* 159 */     if ((this.digest == null) && (other.digest != null)) {
/* 160 */       this.digest = other.digest;
/*     */     }
/* 162 */     if ((this.rawSize == null) && (other.rawSize != null)) {
/* 163 */       this.rawSize = other.rawSize;
/*     */     }
/* 165 */     return this;
/*     */   }
/*     */   
/*     */   public void renameTo(String newPath) throws IOException {
/* 169 */     if (!this.path.equals(newPath)) {
/* 170 */       File newFile = new File(newPath);
/* 171 */       FileUtils.moveFile(this.file, newFile);
/* 172 */       this.path = newPath;
/* 173 */       this.file = newFile;
/*     */     }
/*     */   }
/*     */   
/*     */   public String toString() {
/* 178 */     return Objects.toStringHelper(this).add("path", this.path).add("size", this.rawSize).add("compressed", this.compressed).toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/Blob.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */